﻿using System;
using System.Data;
using System.Collections.Generic;
using SobFS.Model;
using SobFS.DALFactory;
using SobFS.IDAL;
using SobFS.Common.Database;
using SobFS.Common;

namespace SobFS.BLL
{
    /// <summary>
    /// syspermission
    /// </summary>
    public partial class SysPermission
    {
        private readonly ISysPermission dal = DataAccess.CreateSysPermission();

        public SysPermission()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SobFS.Model.SysPermission model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SobFS.Model.SysPermission model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SobFS.Model.SysPermission GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public SobFS.Model.SysPermission GetModelByCache(int Id)
        {

            string cacheKey = typeof(SobFS.Model.SysPermission).Name + Id;
            object objModel = DbCacheHelper.GetCache(cacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int modelCache = ConfigHelper.GetConfigInt("modelCache");
                        DbCacheHelper.SetCache(cacheKey, objModel, DateTime.Now.AddMinutes(modelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (SobFS.Model.SysPermission)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<SobFS.Model.SysPermission> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<SobFS.Model.SysPermission> DataTableToList(DataTable dt)
        {
            List<SobFS.Model.SysPermission> modelList = new List<SobFS.Model.SysPermission>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                SobFS.Model.SysPermission model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new SobFS.Model.SysPermission();
                    if (dt.Rows[n]["Id"] != null && dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    if (dt.Rows[n]["GroupId"] != null && dt.Rows[n]["GroupId"].ToString() != "")
                    {
                        model.GroupId = int.Parse(dt.Rows[n]["GroupId"].ToString());
                    }
                    if (dt.Rows[n]["FunctionMenuId"] != null && dt.Rows[n]["FunctionMenuId"].ToString() != "")
                    {
                        model.FunctionMenuId = int.Parse(dt.Rows[n]["FunctionMenuId"].ToString());
                    }
                    if (dt.Rows[n]["Permission"] != null && dt.Rows[n]["Permission"].ToString() != "")
                    {
                        model.Permission = dt.Rows[n]["Permission"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

